<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            margin: 200px;
        }

        span {
            display: inline-block;
            width: 40px;
            height: 40px;
            background-color: #333;
            font-size: 20px;
            color: #fff;
            text-align: center;
            line-height: 40px;
        }
    </style>
</head>

<body>
    <div>

        <span class="hour">1</span>
        <span class="minute">2</span>
        <span class="second">3</span>



    </div>

    <script>
        //获取元素
        var day = document.querySelector('day');
        var hour = document.querySelector('.hour');
        var minute = document.querySelector('.minute');
        var second = document.querySelector('.second');


        var inputTime = +new Date('2021-11-27 22:59:59');  //返回是用户输入时间总的毫秒数
        countDown();  //我们先调用一次这个函数，防止第一次刷新有空白
        //每隔一秒就开启定时器，setInterval,但是由于一秒之后才会开启，所以前面一秒会显示空白
        //所以开启定时器之前先要调用一次
        setInterval(countDown, 1000);

        function countDown(time) {
            var nowTime = +new Date(); //返回当前时间的总的毫秒数
            var times = (inputTime - nowTime) / 1000;  //times是剩余时间的秒  1秒=1000毫秒

            var h = parseInt(times / 60 / 60 % 24); //小时
            h = h < 10 ? '0' + h : h;
            hour.innerHTML = h;  //把剩余的小时给盒子盒子
            var m = parseInt(times / 60 % 60); //分数
            m = m < 10 ? '0' + m : m;
            minute.innerHTML = m;
            var s = parseInt(times % 60); //当前的秒
            s = s < 10 ? '0' + s : s;
            second.innerHTML = s;

        }



    </script>
</body>

</html>